/**
 * slider.js
 * www.html
 * main
 */

$(function() {
	var $slider = $('#J_slider_min'),
		$slider_box = $slider.find('ul'),
		$slider_item = $slider.find('li'),
		len = $slider_item.length - 1,
		step = $slider_item.width(),
		$trigger = $('#J_slider_trigger').find('a'),
		i = 0;
	$slider_box.width($slider_item.outerWidth(true)*$slider_item.length);
	$trigger.on('mouseover',function(){
		var $this = $(this),
			i = $trigger.index($this);
		move(i);
	});

	function move(i){
		$slider_box.stop(true,true).animate({'left':-i*step});
		$trigger.eq(i).addClass('cur').siblings('a').removeClass('cur');
	}

	function autoMove(){
		flag = setInterval(function(){
			if(i >= len){
				i = 0;
			}else{
				i++;
			}
			move(i)
		},3000);
	}

	autoMove();

	$slider.hover(function(){
		clearInterval(flag);
	},function(){
		autoMove();
	});

    // menu
    var $menu = $('#J_menu'),
        $trigger_menu = $menu.find('li'),
        $subMenu = $menu.find('.sub_item'),
        timer;
    $menu.hover(function(){
        $subMenu.stop(true,true).animate({
            display:'block',
            width:552
        },{
            duration:300
        })
    },function(){        
        $subMenu.stop(true,true).animate({
            display:'none',
            width:0
        },{ 
            duration:200
        })
    });
    $trigger_menu.hover(function(){
        var $this = $(this),
            $subContent = $this.find('.sub-in'),
            $subMenuRel = $this.find('.sub_item');
        $subMenu.hide();
        $subContent.show();
        $subMenuRel.show();
        $this.addClass('open');
    },function(){
        var $this = $(this),
            $subMenuRel = $this.find('.sub_item'),
            $subContent = $this.find('.sub-in');
        $this.removeClass('open');  
        $subMenuRel.hide();      
        $subContent.hide();
    });

    $(".J_search").placeholder({
        className: 'filter-text-placeholder'
    }).on('focusin', function() {
        $(this).addClass('focusin');
    }).on('focusout', function() {
        $(this).removeClass('focusin');
    });



    // tao tab
    $('.Tab').find('.tab-hd li').on('mouseover',function(e){
        e.preventDefault();
        var $this = $(this),
            index = $this.index(),
            $item = $this.parents('.Tab').find('.tab-box .tab-item');
        $this.addClass('cur').siblings('li').removeClass('cur');
        $item.eq(index).show().siblings('.tab-item').hide();
    })
    //tao history
    var $slider = $('.tao-slider');
    var $sliderNav = $slider.find('.tao-slider-nav > .tao-slider-nav-li');
    var $sliderCont = $slider.find('.tao-slider-item');
    window.NS.public.tab($sliderNav, $slider.find('.tao-slider-item'), 'cur', 'tao-slider-item-cur', $slider.find('>.slider-prev'), $slider.find('>.slider-next'), function() {
        var link = $(this).find('a').attr('href');
    });
    var link = $sliderNav.eq(0).find('a').attr('href');
    var $this = $('#J_other_view');
    var lineW = 58;
    var totleNum = $this.find('img').length;

    $('#J_prev').click(function(){
        if(!$this.is(":animated")){
            if(Math.abs($this.position().left) <= 0){
                return false;
            }
            $this.animate({
                left: '+=58'
            }, 300);
        }


    });
    $('#J_next').click(function(){
        if(!$this.is(":animated")){
            if(Math.abs($this.position().left) >= Math.abs(lineW * (totleNum-5))){
                return false;
            }
            $this.animate({
                left: '-=58'
            }, 300);
        }
    });
    // tag cloud
    $('#J_tuanTag').size()?tagCloud("J_tuanTag"):'';
});
//slider分页 和 切换
var $sliderList = $('.slider');
$sliderList.length && $('.slider').each(function() {
    var $slider = $(this);
    var $sliderNav = $slider.find('.slider-nav > .slider-nav-li');
    window.NS.public.tab($sliderNav, $slider.find('.slider-item'), 'cur', 'slider-item-cur', $slider.find('>.slider-prev'), $slider.find('>.slider-next'), function(pageHTML) {
        $slider.find('.slider-page').html(pageHTML);
    });
    $slider.find('.slider-page').on('click', 'a[class!=more]', function(e) {
        e.preventDefault();
        var pageCont = $slider.find('.slider-item:visible');
        var pageHeight = pageCont.innerHeight();
        if (!$(this).hasClass('cur')) {
            var page = ($(this).html() | 0) - 1;
            pageCont.animate({
                scrollTop: page * pageHeight
            }, 300);
            $(this).addClass('cur').siblings('.cur').removeClass('cur');
        }
    });
});
